package com.kantboot.business.common.web.i18n.controller;

import com.alibaba.fastjson2.JSONArray;
import com.kantboot.business.common.dao.i18n.repository.BusCommonCarouselImageI18nRepository;
import com.kantboot.business.common.domain.entity.BusCommonCarouselImage;
import com.kantboot.business.common.domain.i18n.BusCommonCarouselImageI18n;
import com.kantboot.business.common.service.IBusCommonCarouselImageService;
import com.kantboot.global.rest.result.CommonSuccessStateCodeAndMsg;
import com.kantboot.util.common.http.HttpRequestHeaderUtil;
import com.kantboot.util.common.result.RestResult;
import com.kantboot.util.core.redis.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/business-common-web/carouselImageI18n")
public class BusCommonCarouselImageI18nController {

    @Resource
    private BusCommonCarouselImageI18nRepository repository;

    @Resource
    private IBusCommonCarouselImageService carouselImageService;

    @Resource
    private HttpRequestHeaderUtil httpRequestHeaderUtil;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据轮播图id查询轮播图国际化信息
     */
    @RequestMapping(value = "/getByCarouselImageId")
    public RestResult<List<BusCommonCarouselImageI18n>> getByCarouselImageId(
            @RequestParam(value = "carouselImageId") Long carouselImageId) {
        List<BusCommonCarouselImageI18n> busCommonCarouselImageI18n = repository.getByCarouselImageId(carouselImageId);
        return RestResult.success(busCommonCarouselImageI18n, CommonSuccessStateCodeAndMsg.GET_SUCCESS);
    }

    /**
     * 根据语言编码获取轮播图
     */
    @RequestMapping(value = "/getAll")
    public RestResult<List<BusCommonCarouselImage>> getByLanguageCode() {
        String languageCode = httpRequestHeaderUtil.getLanguageCode();
        String jsonStr = redisUtil.get("carouselImageI18n:" + languageCode);
        if (jsonStr != null) {
            return RestResult.success(JSONArray.parseArray(jsonStr,BusCommonCarouselImage.class), CommonSuccessStateCodeAndMsg.GET_SUCCESS);
        }
        List<BusCommonCarouselImage> all = carouselImageService.getAll();
        for (BusCommonCarouselImage carouselImage : all) {
            BusCommonCarouselImageI18n carouselImageI18n = repository.getByLanguageCodeAndCarouselImageId(languageCode, carouselImage.getId());
            if (carouselImageI18n == null) {
                continue;
            }
            if (carouselImageI18n.getName()!=null) {
                carouselImage.setName(carouselImageI18n.getName());
            }
            if (carouselImageI18n.getDescription()!=null) {
                carouselImage.setDescription(carouselImageI18n.getDescription());
            }
            if(carouselImageI18n.getFileIdOfImage()!=null){
                carouselImage.setFileIdOfImage(carouselImageI18n.getFileIdOfImage());
            }
        }
        redisUtil.set("carouselImageI18n:" + languageCode, JSONArray.toJSONString(all));
        return RestResult.success(all, CommonSuccessStateCodeAndMsg.GET_SUCCESS);
    }


}
